package com.unittec.drainage.provider.manage.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unittec.drainage.provider.manage.utils.JtsUtils;
import com.unittec.drainage.provider.manage.utils.coordinates.CoordinatesUtils;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.provider.utils.UUIDUtils;
import com.unittec.drainage.sdk.manage.request.Req301020;
import com.unittec.drainage.sdk.manage.request.Req301021;
import com.unittec.drainage.sdk.manage.request.Req301022;
import com.unittec.drainage.sdk.manage.vo.SewagePlantVo;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.drainage.provider.manage.entity.SewagePlant;
import com.unittec.drainage.provider.manage.mapper.SewagePlantMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Jason
 * @date 2021/8/6
 */
@Service
public class SewagePlantService extends ServiceImpl<SewagePlantMapper, SewagePlant> {

    /**
     * 污水厂列表
     *
     * @param req301020
     * @return
     */
    public List<SewagePlantVo> sewagePlantList(Req301020 req301020) {
        List<SewagePlantVo> list = new ArrayList<>();
        QueryWrapper<SewagePlant> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("1=1");
        if (StrUtil.isNotBlank(req301020.getName())) {
            queryWrapper.lambda().like(SewagePlant::getName, req301020.getName());
        }
        queryWrapper.lambda().orderByAsc(SewagePlant::getSort);
        List<SewagePlant> sewagePlantList = this.baseMapper.selectListByParam(queryWrapper);
        for (SewagePlant sewagePlant : sewagePlantList) {
            SewagePlantVo sewagePlantVo = new SewagePlantVo();
            CopyUtils.copyProperties(sewagePlant, sewagePlantVo);
            if (sewagePlant.getGeom() != null) {
                sewagePlantVo.setCoordinates(CoordinatesUtils.GeometrySwitchByCoordinate(sewagePlant.getGeom()));
            }
            list.add(sewagePlantVo);
        }
        return list;
    }

    /**
     * 添加污水厂
     *
     * @param req301021
     */
    @Transactional(rollbackFor = Exception.class)
    public void addSewagePlant(Req301021 req301021) {
        SewagePlant sewagePlant = new SewagePlant();
        sewagePlant.setId(UUIDUtils.getId());
        CopyUtils.copyProperties(req301021, sewagePlant);
        JSONArray coordinates = req301021.getCoordinates();
        List<SewagePlant> sewagePlantList = this.getBaseMapper().validSewagePlantHasExist(JtsUtils.createPoint(coordinates.getDouble(0), coordinates.getDouble(1), JtsUtils.SRID_4547).toText(), null, JtsUtils.SRID_4547);
        if (!CollectionUtils.isEmpty(sewagePlantList)) {
            throw new BusinessException("污水厂坐标已经存在");
        }
        sewagePlant.setGeom(CoordinatesUtils.initPointGeometry(req301021.getCoordinates()));
        this.baseMapper.insert(sewagePlant, JtsUtils.SRID_4547);
    }

    /**
     * 修改污水厂
     *
     * @param req301022
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSewagePlant(Req301022 req301022) {
        String id = req301022.getId();
        SewagePlant sewagePlant = this.getById(id);
        CopyUtils.copyProperties(req301022, sewagePlant);
        JSONArray coordinates = req301022.getCoordinates();
        List<SewagePlant> sewagePlantList = this.getBaseMapper().validSewagePlantHasExist(JtsUtils.createPoint(coordinates.getDouble(0), coordinates.getDouble(1), JtsUtils.SRID_4547).toText(), id, JtsUtils.SRID_4547);
        if (!CollectionUtils.isEmpty(sewagePlantList)) {
            throw new BusinessException("污水厂坐标已经存在");
        }
        sewagePlant.setGeom(CoordinatesUtils.initPointGeometry(req301022.getCoordinates()));
        this.baseMapper.updateById(sewagePlant, JtsUtils.SRID_4547);
    }

    /**
     * 污水厂详情
     *
     * @param id
     * @return
     */
    public SewagePlantVo getSewagePlantDetails(String id) {
        SewagePlantVo sewagePlantVo = new SewagePlantVo();
        SewagePlant sewagePlant = this.baseMapper.selectSewagePlantById(id);
        if (sewagePlant == null) {
            throw new BusinessException("污水厂不存在");
        }
        CopyUtils.copyProperties(sewagePlant, sewagePlantVo);
        if (sewagePlant.getGeom() != null) {
            sewagePlantVo.setCoordinates(CoordinatesUtils.GeometrySwitchByCoordinate(sewagePlant.getGeom()));
        }
        return sewagePlantVo;
    }

}
